package com.xilihui.xlh.business.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.AccountEntity;
import com.xilihui.xlh.core.util.YEventBuses;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/7/10.
 */

public class BillAdapter extends BaseAdapter{
    
    ArrayList<AccountEntity.StaBean> data = new ArrayList<>();
    
    private Context context;
    
    public BillAdapter(Context context,ArrayList<AccountEntity.StaBean> data){
        this.context = context;
        this.data.clear();
        this.data = data;
    }
    
    @Override
    public int getCount() {
        if(data!=null&&data.size()>0){
            return data.size();
        }else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        
        ViewHolder viewHolder = null;
        if(view==null){
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.recycler_item_bill_sift,null);
            viewHolder.textView = (TextView)view.findViewById(R.id.tv_title);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)view.getTag();
        }
        
        if(data.get(i).isSelect()){
            viewHolder.textView.setSelected(true);
        }else {
            viewHolder.textView.setSelected(false);
        }
        
        viewHolder.textView.setText(data.get(i).getName());

        viewHolder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                data.get(i).setSelect(true);
                for (int j=0;j<data.size();j++){
                    if(j!=i){
                        data.get(j).setSelect(false);
                    }
                }
                YEventBuses.post(new YEventBuses.Event("bill_sift").setParams(data.get(i).getType()));
                notifyDataSetChanged();
            }
        });
        
        return view;
    }
    
    class ViewHolder{
        TextView textView;
    }
    
}
